package com.yunfei.procedure;

import oracle.jdbc.OracleTypes;

import java.sql.*;

/**
* @Description: 产品库-产品详情-产品审批-点击通过或者驳回时调用此存储过程
* @Author: zhengyunfei
* @Date: 2019/2/15
* @time:16:44
*/
public class ProductCheckProcedure {
    private Connection conn;
    private CallableStatement stat;
    private ResultSet rs;
   // String procedureSql = "call sp_prod_check(?,?,?,?,?,?)";
    String pkChkLoginSql = "call PKG_YL_INTERFACE.sp_prod_check(?,?,?,?,?,?)";
    /**
    * @Description: 存储过程，填完理财目标的时候出发此存储过程
    * @Param: [url, driverName, username, password, userId]
     * @param userId 理财师id
     * @param userCheckResult 传入用户审核意见 Y:通过，N:驳回
     * @param userCheckMemo 审核意见
     * @param prodId 产品id
    * @return: int 0成功其他失败
    * @Author: zhengyunfei
    * @Date: 2018/4/24
    * @time:13:49
    */
    public ProcedureResultModel callProcedure(String url,String driverName,String username,String password,String userId,String userCheckResult,String userCheckMemo,String prodId) {
        ProcedureResultModel result=new ProcedureResultModel();
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(url, username, password);
            stat = conn.prepareCall(pkChkLoginSql);
            // 一个输入参数和三个输出参数
            stat.setString(1, userId);
            stat.setString(2, userCheckResult);
            stat.setString(3, userCheckMemo);
            stat.setString(4, prodId);
            stat.registerOutParameter(5, OracleTypes.VARCHAR);
            stat.registerOutParameter(6, OracleTypes.VARCHAR);
            stat.execute();
            String returnCode= stat.getString(5);//处理结果 0:失败 1成功
            String returnInfo= stat.getString(6);//处理结果描述
            result.setReturnCode(returnCode);
            result.setReturnInfo(returnInfo);
            System.out.println("sal: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn, stat, rs);
        }
        return result;
    }

    // 关闭连接
    public void close(Connection conn, CallableStatement stat, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                rs = null;
            }
        }
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                stat = null;
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                conn = null;
            }
        }
    }

    public static void main(String[] args) {
       // new GetTouZiZheRenZhengInfoProcedure().callProcedure("20180417132146779447466");
    }
}